// pages/oa/application/userinfo.js
var api = require("../../../utils/api.js");
var util = require('../../../utils/util.js');
var proId = "";
var processType = "";//模板类型
var roleKey = "";//角色key
var appointType = "";//指定类型 role 指定角色 user 指定人员
var examinealltype = "";//是否需要全部同意才通过 是:0  否:1
var ordernum = "";
var oaId = "";
var choiceuser = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    accountInfosList: [{}],
    examinealltype: "",
    appointType: "",
    showType: "apply",
    searchusertype: ""//copy是抄送人 examine 审核人

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: "人员选择"
    });
    ordernum = options.ordernum;
    choiceuser = options.choiceuser;
    oaId = options.oaId;
    if (oaId != "" && oaId != undefined) {
      wx.setNavigationBarTitle({
        title: "审核人"
      });
      //只显示详情
      that.showUserDetail();
      return false;
    }
    proId = options.proId;
    processType = options.processType;
    roleKey = options.roleKey;
    appointType = options.appointType;
    examinealltype = options.examinealltype;
    var searchusertype = options.searchusertype;
    var orgid = wx.getStorageSync('companyId');
    var userId = wx.getStorageSync('userId');
    that.setData({
      appointType: appointType,
      examinealltype: examinealltype,
      searchusertype: searchusertype
    });
    let pages = getCurrentPages();//当前页面
    let prevPage = pages[pages.length - 2];//上一页面
    var userIds = prevPage.data.userIds;
    api.fetchApi2("oaProgress/choiceSignPerson", {
      orgid: orgid,
      userId: userId,
      proId: proId,
      searchusertype: searchusertype,
      processType: processType,
      copyUserId: userIds,
      choiceuser: choiceuser
    }, "POST").then(res => {
      that.setData({
        accountInfosList: res.data.accountInfosList,
        copyUser: res.data.accountInfosList//抄送人
      });
        //重新组装抄送人
    });
  },

  //显示审核人详情
  showUserDetail: function () {
    var that = this;
    api.fetchApi2("myOaApply/showPerson?oaId=" + oaId + "&orderNum=" + ordernum, {
    }, "POST").then(res => {
      that.setData({
        accountInfosList: res.data.accountInfosList,
        showType: "showUser"//抄送人
      });
    });
  },
  //返回上一页
  backPage: function () {
    wx.navigateBack({ changed: true });//返回上一页
  },
  //根据姓名搜索
  searchUserList: function (e) {
    var that = this;
    var list = new Array();
    that.setData({
      accountInfosList: list
    });
    let pages = getCurrentPages();//当前页面
    let prevPage = pages[pages.length - 2];//上一页面
    var userIds = prevPage.data.userIds;
    api.fetchApi2("oaApproval/searchUserList", {
      type: that.data.searchusertype == "copy" ? "all" : "",
      orgid: wx.getStorageSync('companyId'),
      userId: wx.getStorageSync('userId'),
      proId: proId,
      searchVal: e.detail.value,
      processType: processType,
      appointType: appointType,
      roleKey: roleKey,
      searchusertype: that.data.searchusertype,
      copyUserId: userIds
    }, "POST").then(res => {
      that.setData({
        accountInfosList: res.data.data
      })
    });
  },
  //保存选择的用户
  bindFormSubmit: function (e) {
    var that = this;
    var searchusertype = that.data.searchusertype;
    var userids = e.detail.value.users;
    if (userids.length <= 0) {
      util.showMsg("请选择人员", "loading", "none");
      return false;
    }
    let pages = getCurrentPages();//当前页面
    let prevPage = pages[pages.length - 2];//上一页面
    if (searchusertype == "copy") {
      var accountInfosList = prevPage.data.accountInfosList;
      var userIds = prevPage.data.userIds;
      //重新给抄送人员赋值
      for (var i = 0; i < userids.length; i++) {
        var users = userids[i];
        var m = users.split(",");
        var array = { "id": m[0], "name": m[1], "addUser": "ok", "examine_person_type": "copy" };
        accountInfosList.push(array);
        userIds.push(m[0]);
      }
      prevPage.setData({
        accountInfosList: accountInfosList,
        userIds: userIds
      });
      wx.navigateBack({
        url: "print"
      });

    } else {
      //临时保存审核人员
      var osApprovalsList = prevPage.data.osApprovalsList;
      for (var i = 0; i < osApprovalsList.length; i++) {
        var proIdOld = osApprovalsList[i].proId;
        if (proIdOld == proId) {
          osApprovalsList[i].choice_user_ids = userids;
        }
      }
      prevPage.setData({
        osApprovalsList: osApprovalsList,
      });
      wx.navigateBack({
        url: "print"
      });
      //that.saveUserList(userids);

    }

  },

  //临时保存审核人员
  saveUserList: function (userids) {
    wx.showLoading();
    //保存选择的人员
    wx.request({
      url: getApp().data.servsers + '/oaApply/saveUserList.shtml',
      data: {
        orgid: wx.getStorageSync('companyId'),
        userId: wx.getStorageSync('userId'),
        proId: proId,
        userids: userids
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var result = res.data;
        let pages = getCurrentPages();//当前页面
        let prevPage = pages[pages.length - 2];//上一页面
        if (result == "success") {
          //重新刷新页面
          var flag = true;
          //prevPage.oaApplyPersonList(false);
          wx.navigateBack({
            url: "print"
          });
        } else {
          util.showMsg(result, "loading", "none");
        }
      },
      fail: function () {
        wx.showToast({
          title: '加载数据失败',
          icon: none
        })
      }

    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})